EmaMobile.views.AuthentificationView = Ext.extend( Ext.Panel, {

    layout : 'vbox',
    fullscreen : true,
    initComponent : function() {
        
        this.login;
        this.loginClair;
        this.updateCampagne;
        this.urlPlatforme;
        
        this.Text = new Ext.Panel({
            
            html : 'Authentification',
            cls : 'auth-text'
        });
        
        this.platform = new Ext.form.Text({
            
            allowBlank : false,
            label   : '<span class="auth-field">Plate-forme :</span>',
            originalValue: true,
            value: 'https://demo.pentila.com/EvalComp/webservices',
            labelWidth : '40%'
        });
        
        this.loginField = new Ext.form.Url({
            
            allowBlank : false,
            value: 'eleve1',
            label   : '<span class="auth-field">Login :</span>',
            labelWidth : '40%'
        });

        this.passwordField = new Ext.form.Password({
            
            allowBlank : false,
            label   : '<span class="auth-field">Mot de passe :</span>',
            labelWidth : '40%'
        });
        
        this.loginBtn = new Ext.Button(
                {
                    cls : 'auth-button',
                    maxWidth : 80,
                    text : "<div class='auth-white'>Valider</div>",
                    ui : 'action',
                    scope : this,
                });

        
        this.authentification = new Ext.form.FormPanel({
            items: [
                {
                    xtype: 'fieldset',
                    title: '<div  style=\"font-size:100%\">Authentification</div>',
                    items: [ this.platform, this.loginField, this.passwordField, this.loginBtn ]
                }
            ]
        });
        
        this.toolbar = new Ext.Toolbar({
            title : 'Param&#233;trer',
        });

        
        this.dockedItems = [ this.toolbar ],

        this.items = [ this.authentification ],
                
        //console.log("AuthentificationView");

        EmaMobile.views.AuthentificationView.superclass.initComponent.call(this);
                        
        this.loginBtn.addListener('tap',function(){
                EmaMobile.views.authentificationView.authentifier();
            });
        },
        
        authentifier : function(cle) {
            
            if (!cle){
                this.urlPlatforme = this.platform.getValue();
                this.loginClair = this.loginField.getValue();
                 var pwd = this.passwordField.getValue();
                 var logChain = this.loginClair+':'+pwd;
                 this.login = Ext.util.base64.encode(logChain);
                 this.updateCampagne = true;
                 var message = new Ext.MessageBox();
                 //Il faut que s'il y a pas un login et mdp mis et montrer un erreur
                 message.show({
                     title: 'Information',
                     msg: '<div>Enregistrer votre login et mot de passe ?</div>',
                     buttons: [{text:'Oui',itemId:'oui'},{text:'Non',itemId:'non'}],
                     fn : function(itemId){
                         if (itemId == 'oui'){
                             window.localStorage.clear();
                             EmaMobile.stores.localStore.load();
                             EmaMobile.stores.localStore.add({
                                 cle : EmaMobile.views.authentificationView.login,
                                 uid : EmaMobile.views.authentificationView.loginClair,
                                 url : EmaMobile.views.authentificationView.urlPlatforme
                             });
                             EmaMobile.stores.localStore.sync();
                         }
                     }
                 });
                 message.doComponentLayout();
            }
            else {
                this.login = cle.data.cle;
                this.loginClair = cle.data.uid;
                this.urlPlatforme = cle.data.url;
                this.updateCampagne = true;
            }
            //console.log(this.urlPlatforme+'/getEvaluationsWithTodo');
           
            //console.log("defaultHeaders = {Authorization : Basic " + this.login + " accept : application/json");
            Ext.Ajax.defaultHeaders = {"Authorization" : "Basic " + this.login,  " accept": "application/xml" };
            
            Ext.Ajax.request({
                         url : this.urlPlatforme+'/getEvaluationsWithTodo?contentType=xml',
                         scope : this,
                         success : function(response, opts) {
                             EmaMobile.views.authentificationView.loginBtn.disabled = true;
                            Ext.dispatch({
                            	 controller: EmaMobile.controllers.controller,
                                 action: 'first-page',
                                 //response: response.responseText,
                                 //updateCampagnes: EmaMobile.views.authentificationView.updateCampagne,
                                 //direction : 'left' 
                             });
                                 

                         },
                         failure : function(response, opts) {
                             
                                 var message = new Ext.MessageBox();
                                 message.show({
                                     title: 'Information',
                                     msg: '<div>Erreur d\'authentification</div>',
                                     buttons: [{text:'OK',itemId:'ok'}],
                                     fn : function(itemId){
                                         EmaMobile.views.authentificationView.loginField.reset();
                                         EmaMobile.views.authentificationView.passwordField.reset();
                                     }
                                 });
                                 message.doComponentLayout();
                                 
                                  //console.log("ERROR : "+ response.responseText);
                                 
                         }
                     });
         }
});
